package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TDatabase;
import com.ruoyi.system.domain.bo.TDatabaseBo;
import com.ruoyi.system.domain.vo.TDatabaseVo;
import com.ruoyi.system.mapper.TDatabaseMapper;
import com.ruoyi.system.service.ITDatabaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户或者部门对应库信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-14
 */
@RequiredArgsConstructor
@Service
public class TDatabaseServiceImpl implements ITDatabaseService {

    private final TDatabaseMapper baseMapper;

    /**
     * 查询用户或者部门对应库信息
     */
    @Override
    public TDatabaseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户或者部门对应库信息列表
     */
    @Override
    public TableDataInfo<TDatabaseVo> queryPageList(TDatabaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDatabase> lqw = buildQueryWrapper(bo);
        Page<TDatabaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户或者部门对应库信息列表
     */
    @Override
    public List<TDatabaseVo> queryList(TDatabaseBo bo) {
        LambdaQueryWrapper<TDatabase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDatabase> buildQueryWrapper(TDatabaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDatabase> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDatabaseType()), TDatabase::getDatabaseType, bo.getDatabaseType());
        lqw.eq(StringUtils.isNotBlank(bo.getName()), TDatabase::getName, bo.getName());
        lqw.eq(bo.getCorrespondingType() != null, TDatabase::getCorrespondingType, bo.getCorrespondingType());
        lqw.eq(bo.getCorrespondingId() != null, TDatabase::getCorrespondingId, bo.getCorrespondingId());
        lqw.eq(TDatabase::getDeleted, "0");
        return lqw;
    }

    /**
     * 新增用户或者部门对应库信息
     */
    @Override
    public Boolean insertByBo(TDatabaseBo bo) {
        TDatabase add = BeanUtil.toBean(bo, TDatabase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户或者部门对应库信息
     */
    @Override
    public Boolean updateByBo(TDatabaseBo bo) {
        TDatabase update = BeanUtil.toBean(bo, TDatabase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDatabase entity){
        //TODO 做一些数据校验,如唯一约束
        // 如果是修改，判断是否使用，如果使用了，就不能修改了，同一个类型，只能有一个
        QueryWrapper<TDatabase> q = new QueryWrapper<>();
        q.eq("database_type", entity.getDatabaseType());
        q.eq("corresponding_id", entity.getCorrespondingId());
        q.eq("corresponding_type", entity.getCorrespondingType());
        q.ne(entity.getId() != null, "id", entity.getId());
        q.eq("deleted", "0");
        long cnt =  this.baseMapper.selectCount(q);
        if (cnt > 0) {
            throw new ServiceException("您填写的信息已经存在!");
        }

    }

    /**
     * 批量删除用户或者部门对应库信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
